import { Object3D, Vector3 } from "three";
import { IEditorHistoryOptions } from "../EditorHistory";
import EditorCommand, { EnumEditorCommandName, EnumEditorCommandType, IEditorCommandOutput } from "./EditorCommands";
export interface IEditorSetScaleCommandsOptions extends IEditorHistoryOptions {
    object: Object3D,
    newScale: Vector3,
    oldScale: Vector3
}
export const EVENT_EDITOR_SET_SCALE_COMMANDS = "EVENT_EDITOR_SET_SCALE_COMMANDS"
export default class EditorSetScaleCommands extends EditorCommand {
    object: Object3D;
    newScale: Vector3 = new Vector3();
    oldScale: Vector3 = new Vector3();
    constructor(options: IEditorSetScaleCommandsOptions) {
        super(options);
        let { object, newScale, oldScale } = options;
        this.type = EnumEditorCommandType.SetScaleCommand;
        this.name = EnumEditorCommandName.SetScale;
        this.object = options.object;
        if (object !== undefined && newScale !== undefined) {
            this.oldScale = object.scale.clone();
            this.newScale = newScale.clone();
        }
        if (oldScale !== undefined) {
            this.oldScale = oldScale.clone();
        }
    }
    execute() {
        this.object.scale.copy(this.newScale);
        this.object.updateMatrixWorld(true);
        this.editor.$emit(EVENT_EDITOR_SET_SCALE_COMMANDS, this.newScale)
    }
    undo() {
        this.object.scale.copy(this.oldScale);
        this.object.updateMatrixWorld(true);
        this.editor.$emit(EVENT_EDITOR_SET_SCALE_COMMANDS, this.oldScale)
    }
    update(command: EditorSetScaleCommands) {
        this.newScale.copy(command.newScale);
    }

    toJSON() {
        const output: IEditorCommandOutput = {
            ...super.toJSON(),
            uuid: this.object.uuid,
            oldScale: this.oldScale.toArray(),
            newScale: this.newScale.toArray()
        };
        return output;
    }

    fromJSON(json: IEditorCommandOutput) {
        super.fromJSON(json);
        let object = this.editor.objectByUuid(json.uuid!);
        if (object) {
            this.object = object;
            this.oldScale = new Vector3().fromArray(json.oldScale!);
            this.newScale = new Vector3().fromArray(json.newScale!);
        } else {
            throw new Error('找不到uuid对应的对象')
        }
    }

}